package blah;

import java.beans.PropertyChangeEvent;

import junit.framework.TestCase;
import javax.swing.Timer;
import javax.swing.SwingUtilities;
import com.gargoylesoftware.base.testing.EventCatcher;

public class Test extends TestCase {

	Timer timer;
	EchoBean echoBean;
	
	protected void setUp() throws Exception {
		super.setUp();
		timer = new Timer(500, null);
		echoBean = new EchoBean();
		timer.setRepeats(false);
	}
	
	public void testTimer() throws Exception {
		EventCatcher catcher = new EventCatcher();
		catcher.listenTo(timer);
		timer.start();
		Thread.sleep(1000);
		assertEquals(1, catcher.getEventCount());
	}
		
	public void testEchoBean() throws Exception {
		Runnable eventFirer = new Runnable() {
			PropertyChangeEvent event = new PropertyChangeEvent(this, "Blah", null, "Kuck");
  		public void run() {
  			echoBean.propertyChange(event);
  		}
  	}; 
  	Runnable sync = new Runnable() {
  		public void run() { }
  	}; 
		EventCatcher catcher = new EventCatcher();
		catcher.listenTo(echoBean);
		assertEquals(0, catcher.getEventCount());
		int reps = 1000;
		for (int i = 0; i < reps; i ++) {
			SwingUtilities.invokeAndWait(eventFirer);
		}
		SwingUtilities.invokeAndWait(sync);
		assertEquals(reps, catcher.getEventCount());
	}
}
